<template>
  <div class="login">
    <div class="login_box">
      <div class="avator_box">
        <img src="../assets/logo.png">
      </div>
      <el-form class="user_form" :rules="form_rules" ref="login_ref" :model="user_form" label-width="0 ">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont iconuser" v-model="user_form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont iconsuo" type="password" v-model="user_form.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="Login">登录</el-button>
          <el-button type="info" @click="ResetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default{
    data(){
      return {
        user_form:{
          username:'admin',
          password:'123456'
        },
        form_rules:{
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
          ],
        }
      }
    },
    methods:{
      Login(){
        this.$refs.login_ref.validate(async (valid)=>{
          if(!valid) return
          const res=await this.$http.post('login',this.user_form)
          // console.log(res);
          if(res.data.meta.status!==200) return this.$message.error(res.data.meta.msg)
          this.$message.success(res.data.meta.msg)
          window.sessionStorage.setItem('token',res.data.data.token)
          this.$router.push('/home')
        })
      },
      ResetLoginForm(){
        this.$refs.login_ref.resetFields()
      }
    }
  }
</script>

<style lang="less" scoped>
.login{
  background-color: #274b6c;
  height: 100%;
  .login_box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 5px;
    .avator_box{
        padding: 10px;
        width: 130px;
        height: 130px;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        background-color: #fff;
        position: absolute;
        top: 0;
        left: 50%;
        transform: translate(-50%,-50%);
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .user_form{
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btns{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>